package com.xugongzhang.spring.beans;

import lombok.Data;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * @author X
 * @date 2022/12/3 22:10
 */
@Data
public class Dog implements ApplicationContextAware {
    
//    @Value("zhangSan") // 直接赋值
//    @Value("#{20-2}") // 用SpEL表达式,可以计算
    @Value("${dog.age:18}") // 读取配置文件
    private int age;
    
    private ApplicationContext applicationContext;

    public Dog() {
        System.out.println("Dog constructor...");
    }
    
    @PostConstruct
    public void init() {
        System.out.println("Dog post constructor...");
    }
    
    @PreDestroy
    public void destroy() {
        System.out.println("Dog preDestroy...");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
